package me.zhengjie.modules.system.rest

import me.zhengjie.common.exception.BadRequestException
import me.zhengjie.logging.annotation.Log
import me.zhengjie.modules.system.domain.DictDetail
import me.zhengjie.modules.system.service.DictDetailService
import me.zhengjie.modules.system.service.dto.DictDetailQueryCriteria

import io.swagger.v3.oas.annotations.media.Schema
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

/**
 * @author Kuki Wu
 * @date 2019-04-10
 */
@RestController
@Schema(description = "系统：字典详情管理")
@RequestMapping("/api/dictDetail")
class DictDetailController(private val dictDetailService: DictDetailService) {
    @Log("查询字典详情")
    @Schema(description = "查询字典详情")
    @GetMapping
    fun getDictDetails(
        criteria: DictDetailQueryCriteria?,
        @PageableDefault(sort = ["sort"], direction = Sort.Direction.ASC) pageable: Pageable?
    ): ResponseEntity<*> {
        return ResponseEntity(dictDetailService.queryAll(criteria!!, pageable), HttpStatus.OK)
    }

    @Log("查询多个字典详情")
    @Schema(description = "查询多个字典详情")
    @GetMapping(value = ["/map"])
    fun getDictDetailMaps(
        criteria: DictDetailQueryCriteria,
        @PageableDefault(sort = ["sort"], direction = Sort.Direction.ASC) pageable: Pageable?
    ): ResponseEntity<*> {
        val names = criteria.dictName!!.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        val map: MutableMap<String, Any?> = HashMap(names.size)
        for (name in names) {
            criteria.dictName = name
            map[name] = (dictDetailService.queryAll(criteria, pageable) as Map<String, Any?>)["content"]
        }
        return ResponseEntity<Map<String, Any?>>(map, HttpStatus.OK)
    }

    @Log("新增字典详情")
    @Schema(description = "新增字典详情")
    @PostMapping
    @PreAuthorize("@el.check('dict:add')")
    fun create(@Validated @RequestBody resources: DictDetail): ResponseEntity<*> {
        if (resources.id != null) {
            throw BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID")
        }
        return ResponseEntity(dictDetailService.create(resources), HttpStatus.CREATED)
    }

    @Log("修改字典详情")
    @Schema(description = "修改字典详情")
    @PutMapping
    @PreAuthorize("@el.check('dict:edit')")
    fun update(
        @Validated(
            DictDetail.Update::class
        ) @RequestBody resources: DictDetail?
    ): ResponseEntity<*> {
        dictDetailService.update(resources!!)
        return ResponseEntity<Any?>(HttpStatus.NO_CONTENT)
    }

    @Log("删除字典详情")
    @Schema(description = "删除字典详情")
    @DeleteMapping(value = ["/{id}"])
    @PreAuthorize("@el.check('dict:del')")
    fun delete(@PathVariable id: Long?): ResponseEntity<*> {
        dictDetailService.delete(id!!)
        return ResponseEntity<Any?>(HttpStatus.OK)
    }

    companion object {
        private const val ENTITY_NAME = "dictDetail"
    }
}